import time
import requests
import json
from PyCmpltrtok.common import sep
from langchain.utilities.bing_search import BingSearchAPIWrapper

# search_url = "https://api.bing.microsoft.com/"
# search_url = "https://api.bing.microsoft.com/v7.0/search"
search_url = "https://api.bing.microsoft.com/v7.0/custom/search"
subscription_key = '8e2dc08127314251a9ec52fde6d37546'
custom_config = "716c46ae-d1fa-40e7-ae91-23bb113412f3"
    

def bing_search(text, result_len=5, **kwargs):
    search = BingSearchAPIWrapper(bing_subscription_key=subscription_key,
                                  bing_search_url=search_url)
    search.search_kwargs['customConfig'] = custom_config
    return search.results(text, result_len)


if '__main__' == __name__:
    
    import argparse
    
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('names', help='key words to search', nargs='*', default=["最终幻想8重制版"])
    args = parser.parse_args()
    names = args.names
    
    for name in names:
        sep(name)
        search_results = bing_search(name)

        print(search_results)
        with open(f'{name}.tmp.json', 'w', encoding='utf8') as f:
            json.dump(search_results, f, ensure_ascii=False)
            
        time.sleep(1.0)
